package cn.iocoder.yudao.module.research.service.alert;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.infra.llm.ILLMService;
import cn.iocoder.yudao.module.infra.llm.LLMServiceFactory;
import cn.iocoder.yudao.module.research.controller.admin.alert.vo.AlertChatReqVO;
import cn.iocoder.yudao.module.research.dal.mysql.alert.AlertMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
@Service
public class ChatService {

    @Resource
    private LLMServiceFactory llmServiceFactory;

    @Resource
    private AlertMapper alertMapper;

    private String syslogTemplate;

    private String chatTemplate;

    @PostConstruct
    public void init() {
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("prompt/analysis-syslog-template.txt")) {
            syslogTemplate = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("初始化syslog解析提示词模板失败", e);
        }
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("prompt/chat-alert-template.txt")) {
            chatTemplate = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            log.error("初始化告警研判提示词模板失败", e);
        }
    }

    public String createChat(AlertChatReqVO chatReqVO) {
        ILLMService service = llmServiceFactory.getService();

        /*service.streamChatResponse(null, chatReqVO.getMessage(), chatReqVO.getMessage(), null,
                llmRes -> {
                    final String replyText = llmRes.getReplyText();
                    final Boolean textDone = llmRes.isDone();
                    if (textDone) {
                        final String fullReplyText = String.join("", messageCtx.getReplyTexts());
                    }
                });*/

        if (StrUtil.isNotEmpty(chatReqVO.getMessage())) {
            String replyText = service.textChatResponse(null, chatReqVO.getMessage(), null);
            return replyText;
        }
        return null;
    }

    public String createChat(String message, String logContent, String logAiResponse) {
        ILLMService service = llmServiceFactory.getService();
        String prompt = chatTemplate.replaceAll("\\{syslog}", logContent)
                .replaceAll("\\{data}", logAiResponse)
                .replaceAll("\\{question}", message);

        return service.textChatResponse(null, prompt, null);
    }

    public String analysisSyslog(String content) {
        ILLMService service = llmServiceFactory.getService();
        String prompt = syslogTemplate + content;
        if (StrUtil.isNotEmpty(content)) {
            String replyText = service.textChatResponse(null, prompt, null);
            return replyText;
        }
        return null;
    }
}
